package com.mccken.eshop.common.core;

import java.util.HashMap;
import java.util.Map;

import net.sf.cglib.beans.BeanCopier;

/**
 * BeanCopier工具类 
 * 
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2022-08-27 23:09
 **/
public class BeanCopierUtil {
	
	private static Map<String, BeanCopier> beanCopierCacheMap = new HashMap<>();

	/**
	 * 将source对象的属性拷贝到target对象中去
	 * 
	 * @param source
	 * @param target
	 */
	public static void copyProperties(Object source, Object target) {
		String cacheKey = source.getClass().toString() + target.getClass().toString();
		BeanCopier beanCopier = null;
		
		if (!beanCopierCacheMap.containsKey(cacheKey)) {
			synchronized (BeanCopierUtil.class) {
				if (!beanCopierCacheMap.containsKey(cacheKey)) {
					beanCopier = BeanCopier.create(source.getClass(), target.getClass(), false);
					beanCopierCacheMap.put(cacheKey, beanCopier);	
				} else {
					beanCopier = beanCopierCacheMap.get(cacheKey);
				}
			}
		} else {
			beanCopier = beanCopierCacheMap.get(cacheKey);
		}
		
		beanCopier.copy(source,target, null);
	}
}
